/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

import MNetSockets.DataChanel.IUDPDataReader;
import Tool.FileCodeMaker;
import UDPFiles.IUDPFileStateEvent;
import UDPFiles.UDPCmdFinal;
import UDPFiles.UDPFileServer;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wcss
 */
public class UDPFileTest extends javax.swing.JFrame implements IUDPDataReader, IUDPFileStateEvent, Runnable {

    private UDPFileServer ufsB = null;
    File ff = null;
    private long time = 0;

    /**
     * Creates new form UDPFileTest
     */
    public UDPFileTest() {
        try {
            initComponents();
            ff = new File("/home/wcss/视频/QvodMovies/346362,105.jpg");
            new UDPFileShareTest(62510, ff).setVisible(true);
            //new UDPFileShareTest(62511, ff).setVisible(true);
            ufsB = new UDPFileServer(62512, "/home/wcss/UDPTest_B");
            ufsB.stateEvent = this;
            ufsB.dataReceiveEvent = this;
            Thread tt = new Thread(this);
            tt.setDaemon(true);
            tt.start();
        } catch (Exception ex) {
            Logger.getLogger(UDPFileTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblDown = new javax.swing.JLabel();
        lblUpload = new javax.swing.JLabel();
        btnStart = new javax.swing.JButton();
        lblTime = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblDown.setText("jLabel1");

        lblUpload.setText("jLabel1");

        btnStart.setText("开始");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        lblTime.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(474, 474, 474)
                        .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(315, 315, 315)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblDown, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                            .addComponent(lblUpload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(304, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(lblDown, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUpload, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(btnStart)
                .addContainerGap(124, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        try {
            // TODO add your handling code here:
            ufsB.StartDownTask(FileCodeMaker.makeFileCode(ff), new File("/home/wcss/文档/testmovie.jpg"), new SocketAddress[]{ new InetSocketAddress("127.0.0.1", 62510)});
            time = 0;
        } catch (Exception ex) {
            Logger.getLogger(UDPFileTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnStartActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UDPFileTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UDPFileTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UDPFileTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UDPFileTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UDPFileTest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStart;
    private javax.swing.JLabel lblDown;
    private javax.swing.JLabel lblTime;
    private javax.swing.JLabel lblUpload;
    // End of variables declaration//GEN-END:variables

    @Override
    public void dataRead(Object o, SocketAddress sa, Object o1) {
    }

    @Override
    public void changeState(Object sender, String statecode, String text) {
        System.out.println(statecode + "," + text);
        if (statecode.equals(UDPCmdFinal.progressState)) {
            if (text.equals("500")) {
                time = 0;
            }
            this.lblDown.setText("已下载：" + (Long.parseLong(text) / 1024) + "K " + (int) ((((double) Long.parseLong(text)) / (double) ff.length()) * 100) + "%");
        } else if (statecode.equals(UDPCmdFinal.requestState)) {
            this.lblUpload.setText("当前正在上传：" + text + "号数据包");
        } else if (statecode.equals(UDPCmdFinal.finishState))
        {
            this.lblDown.setText("下载完成！");
        }
    }

    @Override
    public void run() {
        while (true) {
            time++;
            this.lblTime.setText(time + "");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(UDPFileTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
